package com.foo.cms.action;

import com.foo.cms.reqvo.VideoReq;
import com.foo.cms.service.ArticleService;
import com.foo.cms.service.CategoryService;
import com.foo.cms.service.PartService;
import com.foo.cms.service.RecommendService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * Created with IntelliJ IDEA.
 * User: romkk
 * Date: 12-6-24
 * Time: 下午4:23
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/video")
public class VideoAction {
    private static final Logger logger = LoggerFactory.getLogger(VideoAction.class);

    @Resource
    private ArticleService articleService;

    @Resource
    private CategoryService categoryService;

    @Resource
    private PartService partService;

    @Resource
    private RecommendService recommendService;


    @RequestMapping("/index")
    public String index() {
        return "video/index.jsp";
    }

    @RequestMapping("/input")
    public ModelAndView input() {
        ModelAndView mv = new ModelAndView("video/input.jsp");
        mv.addObject("parts", partService.listLeaf());
        mv.addObject("recommends", recommendService.listRec());
        return mv;
    }

    @RequestMapping("/save")
    public ModelAndView save(VideoReq req) {
        ModelAndView mv = new ModelAndView("video/input.jsp");
        try {
            logger.info(req.toString());
            UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String uploader = userDetails.getUsername();
            articleService.addVideoArticle(req, uploader);
            mv.setViewName("redirect:/article/index.shtml?msg=OK");
        } catch (Exception e) {
            mv.addObject("categories", categoryService.listAll());
            mv.addObject("parts", partService.listLeaf());
            mv.addObject("recommends", recommendService.listRec());
            mv.addObject("data", req);
        }
        return mv;
    }

    @RequestMapping("/editsave")
    @ResponseBody
    public String editSave(VideoReq req, String uid) {
        logger.info(req.toString());
        articleService.editVideoArticle(req, uid);
        return "编辑成功！";
    }

    @RequestMapping("/list")
    public String list() {
        return "video/list.jsp";
    }
}
